#include "stdafx.h"
#include "timer.h"

namespace Clock
{
	Timer::Timer(Clocks aClock)
	{
		myIsPausedFlag = true;
		myCurrentTime = Time(aClock, 0);
		myElapsedTime = Time(aClock, 0);
	}

	Timer::~Timer()
	{
		myAttachedTimers.clear();
	}

	void Timer::AttachTimer(Timer* aTimer)
	{
		myAttachedTimers.push_back(aTimer);
	}

	void Timer::Update()
	{
		myElapsedTime.SetTime(0);
		for(unsigned int timerIndex=0; timerIndex<myAttachedTimers.size(); timerIndex++)
		{
			myAttachedTimers[timerIndex]->Update();
		}
	}

	bool Timer::Update(const Time &aElapsedTime)
	{
		if(myIsPausedFlag == true)
		{
			myElapsedTime.SetTime(0);
			for(unsigned int timerIndex=0; timerIndex<myAttachedTimers.size(); timerIndex++)
			{
				myAttachedTimers[timerIndex]->Update();
			}
			return false;
		}
		myCurrentTime.Add(aElapsedTime);
		myElapsedTime = aElapsedTime;
		for(unsigned int timerIndex=0; timerIndex<myAttachedTimers.size(); timerIndex++)
		{
			myAttachedTimers[timerIndex]->Update(aElapsedTime);
		}
		return true;
	}

	bool Timer::SetPauseState(const bool &aPausedFlag)
	{
		//bool returnFlag = myIsPausedFlag;
		myIsPausedFlag = aPausedFlag;
		return myIsPausedFlag;
	}
};
